var express = require('express');
var router = express.Router();
const orderService = require('../service/orderService');

//查询所有订单
router.get('/', async function (req, res) {
    let { type, value, current, size } = req.query;
    let searchObj = {};//
    if (type) {
        if (type == "number") {
            searchObj[type] = value;
        } else {
            searchObj[type] = { $regex: value };
        }
    }
    let data = await orderService.find(searchObj, { current: current || 1, size: size || 10 });
    res.send(data);

})
// 新增订单
router.post('/', async function (req, res) {
    let data = await orderService.insert(req.body);
    res.send(data);
})
// 删除订单
router.delete('/:id', async function (req, res) {
    let id = req.params.id;
    let data = await orderService.delete(id);
    res.send(data);
})
// 通过订单的id查询
router.get('/:id', async function (req, res) {
    let id = req.params.id;
    let data = await orderService.findById(id);
    res.send(data);
})

module.exports = router;